package com.order.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.Constant;

import com.UserModule.bean.TbUserBean;
import com.dati.bean.DatiType;
import com.dati.dao.DatiTypeDao;
import com.dianbo.bean.DianboType;
import com.dianbo.dao.DianboTypeDao;
import com.order.bean.TbOrder;
import com.order.bean.TbWuxiaoOrder;
import com.order.dao.TbOrderDao;

public class TbOrderServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public TbOrderServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String method=request.getParameter("method");
		TbUserBean user = (TbUserBean)request.getSession().getAttribute("User");
		if("look".equals(method)){//显示指令
			List list = this.getValue(user);
			List<TbOrder> orderDianboList = (ArrayList<TbOrder>)list.get(0);
			List<TbOrder> orderDatiList = (ArrayList<TbOrder>)list.get(1);
			request.getSession().setAttribute("orderDianboList", orderDianboList);
			request.getSession().setAttribute("orderDatiList", orderDatiList);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("hudong/orderList.jsp");
		}else if("addOrder".equals(method)){//新建指令时的显示
			String formName = request.getParameter("formName");
			String orderType = request.getParameter("orderType");//点播或答题
			String condition="userId="+user.getUserId()+" and orderType=0";
			List<TbOrder> orderDianboList = new ArrayList<TbOrder>();
			orderDianboList = new TbOrderDao().getOrderList("tb_order", "createTime", condition);
			String conditionT="userId="+user.getUserId()+" and orderType=1";
			List<TbOrder> orderDatiList = new ArrayList<TbOrder>();
			orderDatiList = new TbOrderDao().getOrderList("tb_order", "createTime", conditionT);
			
			List<String> orderTypeList = new ArrayList<String>();//存放已设置指令对应的点播或答题的ID
			if(orderDianboList!=null&&orderDianboList.size()>0){
				for(TbOrder orde:orderDianboList){
					if(!orderTypeList.contains(orde.getTypeId())){
						orderTypeList.add(orde.getTypeId());
					}
				}
			}
			if(orderDatiList!=null&&orderDatiList.size()>0){
				for(TbOrder orde:orderDatiList){
					if(!orderTypeList.contains(orde.getTypeId())){
						orderTypeList.add(orde.getTypeId());
					}
				}
			}
			
			List<DatiType> datiTypeList = new ArrayList<DatiType>();
			String conditionDati = "userId="+user.getUserId();
			datiTypeList = new DatiTypeDao().getAllDaTiType("tb_answer_type", "createTime", conditionDati);
			
			List<DianboType> dianboTypeList = new ArrayList<DianboType>();
			dianboTypeList = new DianboTypeDao().getDianboList("tb_qsType_answer", conditionDati, "createTime");
			
			List<DianboType> lastDianboTypeList = new ArrayList<DianboType>();//能进行设置指令的点播类型
			List<DatiType> lastDatiTypeList = new ArrayList<DatiType>();
			if(datiTypeList!=null&&datiTypeList.size()>0){
				for(DatiType dati:datiTypeList){
					if(!orderTypeList.contains(dati.getAnswerTypeId())){
						lastDatiTypeList.add(dati);
					}
				}
			}
			if(dianboTypeList!=null&&dianboTypeList.size()>0){
				for(DianboType dianbo:dianboTypeList){
					if(!orderTypeList.contains(dianbo.getTypeId())){
						lastDianboTypeList.add(dianbo);
					}
				}
			}
			request.getSession().setAttribute("lastDianboTypeList", lastDianboTypeList);
			request.getSession().setAttribute("lastDatiTypeList", lastDatiTypeList);
			response.sendRedirect("hudong/addOrder.jsp?orderType="+orderType+"&num="+new Random().nextInt(1000000)+"&formName="+formName);
		}else if("addOrderSubmit".equals(method)){//新增指令提交
			String orderType = request.getParameter("orderType");//区分是点播或答题
			String orderName = request.getParameter("orderName");
			String typeId = request.getParameter("typeId");
			TbOrder order = new TbOrder();
			order.setCreateTime(Constant.decodeDate());
			order.setOrderType(Integer.parseInt(orderType));
			order.setOrderValue(orderName);
			order.setTypeId(typeId);
			order.setUserId(user.getUserId());
			boolean bo = new TbOrderDao().saveOrder(order);
			if(bo){
				request.getSession().setAttribute("result", "指令设置成功!");
			}else{
				request.getSession().setAttribute("result", "指令设置失败!");
			}
			List list = this.getValue(user);
			List<TbOrder> orderDianboList = (ArrayList<TbOrder>)list.get(0);
			List<TbOrder> orderDatiList = (ArrayList<TbOrder>)list.get(1);
			request.getSession().setAttribute("orderDianboList", orderDianboList);
			request.getSession().setAttribute("orderDatiList", orderDatiList);
			response.sendRedirect("hudong/orderList.jsp");
		}else if("modifyOrder".equals(method)){//修改显示
			String orderId = request.getParameter("id");
			String condition = "orderId="+orderId;
			String formName = request.getParameter("formName");
			List<TbOrder> order = new TbOrderDao().getOrderList("tb_order", "createTime", condition);
			TbOrder tbOrder = order.get(0);
			request.getSession().setAttribute("tbOrder", tbOrder);
			response.sendRedirect("hudong/modifyOrder.jsp?formName="+formName+"&num="+new Random().nextInt(10000000));
		}else if("modifyOrderSubmit".equals(method)){//修改提交
			System.out.println("修改提交");
			String id = request.getParameter("id");
			String orderName = Constant.decodeString(request.getParameter("orderName"));
			System.out.println("orderName=="+orderName);
			TbOrder order = new TbOrder();
			order.setCreateTime(Constant.decodeDate());
			order.setOrderValue(orderName);
			order.setOrderId(Integer.parseInt(id));
			boolean bo = new TbOrderDao().modifyOrder(order);
			if(bo){
				request.getSession().setAttribute("result", "指令修改成功!");
			}else{
				request.getSession().setAttribute("result", "指令修改失败!");
			}
			List list = this.getValue(user);
			List<TbOrder> orderDianboList = (ArrayList<TbOrder>)list.get(0);
			List<TbOrder> orderDatiList = (ArrayList<TbOrder>)list.get(1);
			request.getSession().setAttribute("orderDianboList", orderDianboList);
			request.getSession().setAttribute("orderDatiList", orderDatiList);
			response.sendRedirect("hudong/orderList.jsp");
		}else if("del".equals(method)){
			String id = request.getParameter("id");
			boolean bo = new TbOrderDao().deleteOrder(Integer.parseInt(id));
			if(bo){
				request.getSession().setAttribute("result", "指令删除成功!");
			}else{
				request.getSession().setAttribute("result", "指令删除失败!");
			}
			List list = this.getValue(user);
			List<TbOrder> orderDianboList = (ArrayList<TbOrder>)list.get(0);
			List<TbOrder> orderDatiList = (ArrayList<TbOrder>)list.get(1);
			request.getSession().setAttribute("orderDianboList", orderDianboList);
			request.getSession().setAttribute("orderDatiList", orderDatiList);
			response.sendRedirect("hudong/orderList.jsp");
		}else if("lookWuxiao".equals(method)){//设置无效指令
			String condition="userId="+user.getUserId();
			List<TbWuxiaoOrder> wuxiaoList =new ArrayList<TbWuxiaoOrder>();
			wuxiaoList = new TbOrderDao().getWuxiaoList("tb_invalid", "", condition);
			request.getSession().setAttribute("wuxiaoList", wuxiaoList);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("hudong/setWuxiao.jsp");
		}else if("setWuxiao".equals(method)){//设置无效指令提交
			String wuxiao = Constant.decodeString(request.getParameter("wuxiao"));
			int isreply=Integer.parseInt(request.getParameter("isreply"));
			TbWuxiaoOrder wuxiaoOrder = new TbWuxiaoOrder();
			wuxiaoOrder.setInvalidContent(wuxiao);
			wuxiaoOrder.setUserId(user.getUserId());
			wuxiaoOrder.setStatus(isreply);
			boolean bo = new TbOrderDao().saveWuxiao(wuxiaoOrder);
			if(bo){
				request.getSession().setAttribute("result", "设置成功!");
			}else {
				request.getSession().setAttribute("result", "设置失败!");
			}
			String condition="userId="+user.getUserId();
			List<TbWuxiaoOrder> wuxiaoList =new ArrayList<TbWuxiaoOrder>();
			wuxiaoList = new TbOrderDao().getWuxiaoList("tb_invalid", "", condition);
			request.getSession().setAttribute("wuxiaoList", wuxiaoList);
			response.sendRedirect("hudong/setWuxiao.jsp");
		}
		
	}
	public List getValue(TbUserBean user){
		List list = new ArrayList();
		String condition="userId="+user.getUserId()+" and orderType=0";
		List<TbOrder> orderDianboList = new ArrayList<TbOrder>();
		orderDianboList = new TbOrderDao().getOrderList("tb_order", "createTime", condition);
		String conditionT="userId="+user.getUserId()+" and orderType=1";
		List<TbOrder> orderDatiList = new ArrayList<TbOrder>();
		orderDatiList = new TbOrderDao().getOrderList("tb_order", "createTime", conditionT);
		list.add(orderDianboList);
		list.add(orderDatiList);
		return list;
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-11
	 *下午04:42:14
	 *zzClub
	 *com.order.servlet
	 */
}
